//公用全局变量，方法
export default {
	apihost:(() => {
		let hostname = window.location.host;
		switch (hostname){
            case "test32plan.itrip.com" :{
               return "//192.168.0.111:13299/itrip_travel_helper/";
            }
            case "pre2plan.itrip.com" : {
                return "//pre2plan.itrip.com/itrip_travel_helper/";
            }
            case "plan.itrip.com" : {
                return "//plan.itrip.com/itrip_travel_helper/";
            }
            default :{
                return "http://192.168.0.111:13299/itrip_travel_helper/";
                break;
            }
        }
	})(),
	$get(url,params,_self,method,callback,exp){
		let ajaxurl = this.apihost + url;
		let data = this.getCache(url);
		/*if(data){
			callback(_self,data,false);
		}else{*/
			_self.$http({
				url:ajaxurl,
				params:params,
				method:method,
				timeout:1000000,
				emulateJSON:true,
				emulateHTTP:true
			})
			.then((response) => {
				console.log(response.data);
				callback(_self,response.data,true);
				/*if (!(exp && exp < 0)) this.setCache(url, response.data, exp)
		        let json = {}
		        json.timestamp = this.timestamp()
		        json.data = response.data*/
			},(response) => {
				this.ajaxerr(response);
			})
		//}
	},
	$post(url,params,_self,callback){
		let ajaxurl = this.apihost + url;
		_self.$http.post(ajaxurl,params,{

		}).then((response) => {
			callback(_self, response.data);
		},(response) => {
			this.ajaxerr(response);
		})
	},
	exp: 300,//默认300s缓存数据
	cache:[],
	getCache(url){
		let json = this.cache[url];
		return (json && (json.exp >= this.timestamp())) ? json.data : null
	},
	setCache(url,data,exp){
		// 默认300秒,{data,exp}
    	this.cache[url] = {data: data, exp: this.timestamp() + ((!exp) ? this.exp : exp)}
    	// console.log(this.cache)
	},
	// 时间戳
  	timestamp () {
    	return Math.floor(new Date().getTime() / 1000)
  	},
  	ajaxerr(response){
  		switch(response.status){
			case 0: // 网络不通
				//this.toast('网络暂时未能连通', '', true, 'top')
				break
			case 401: // 未登录
			case 521: // 超时
			case 522: // 错误
				// 退出当前用户帐号,路由到登录页
				//this.init()
				//router.go('/login?redirect=' + encodeURIComponent(window.location.pathname + window.location.search))
				break
			case 523:
				//this.toast('当前时间存在较大偏差')
				break
			case 524:
				//this.toast('未找到相关数据')
				break
			default:
				// 其它错误，暂不处理
				break
    	}
  	}
}